package com.edu.hre.struts.action.dashboard;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.edu.hre.common.entity.BeanChart;
import com.edu.hre.common.entity.BeanChart2;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreJobCandidate;
import com.edu.hre.db.pojo.HreJobVacancy;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.dashboard.DashLeaveForm;
import com.edu.hre.struts.form.dashboard.DashRecruitmentForm;

public class DashRecruitmentAction extends DispatchAction {
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation()); 
		DashRecruitmentForm beanForm = (DashRecruitmentForm) form;
		executeStatic(request);
		return mapping.findForward("dashRecruitment");
	}
	
	public static void executeStatic(HttpServletRequest request){
		
		int totalStatus1 = baseService.countByColumns("status", Compare.EQUAL, 1, "id", HreJobVacancy.class);
		int totalStatus0 = baseService.countByColumns("status", Compare.EQUAL, 0, "id", HreJobVacancy.class);
		if(totalStatus1 + totalStatus0 != 0){
			request.setAttribute("pie_partsNumber", totalStatus1 + ";" + totalStatus0);
			request.setAttribute("pie_partsName", "Active;Inactive");
			request.setAttribute("pie_topic", "Vacancy Status");
			request.setAttribute("pie_totalLeaveDays", (Float.valueOf(totalStatus1 + totalStatus0)));
			
			CommonUtils.println("pie_partsNumber: " + totalStatus1 + ";" + totalStatus0);
			CommonUtils.println("pie_partsName: " + "Active;Inactive");
			CommonUtils.println("pie_totalLeaveDays: " + (Float.valueOf(totalStatus1 + totalStatus0)));
		}
		
		//=============================================================================================
		
		String bar2_partsNumber = "";
		String bar2_partsName = "";
		List<BeanChart2> listBean2 = getListBeans();
		for(int i = 0; listBean2 != null && i < listBean2.size(); i++){
			BeanChart2 bean = listBean2.get(i);
			bar2_partsName += ";" + (bean.getId() == null ? "Unknown" : bean.getId());
			bar2_partsNumber += ";" + bean.getValue();
		}
		bar2_partsName = bar2_partsName.replaceFirst(";", "");
		bar2_partsNumber = bar2_partsNumber.replaceFirst(";", "");
		request.setAttribute("bar2_partsName", bar2_partsName);
		request.setAttribute("bar2_partsNumber", bar2_partsNumber);
		request.setAttribute("bar2_topic", "Positions status");
		CommonUtils.println("bar2_partsName: " + bar2_partsName);
		CommonUtils.println("bar2_partsNumber: " + bar2_partsNumber);
		
		//=============================================================================================
		
		
		
		
	}
	
	private static List<BeanChart2> getListBeans(){
		String sql = "SELECT t.job_title, v.no_of_positions  FROM hrentry_db.hre_job_vacancy v " +  
					"inner join hre_job_title t on t.id = v.job_title_code where v.status = 1";
		List<BeanChart2> listBeans = new ArrayList<BeanChart2>();
		List<Object[]> listRs = baseService.listArrayObject(sql, 0, 0);
		for(int i = 0; listRs != null && i < listRs.size(); i ++){
			try {
				Object[] obj = listRs.get(i);
				Float f = ((Integer) obj[2]) * 1f;
				listBeans.add(new BeanChart2((String) obj[1], f));
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		return listBeans;
	}
}
